﻿using BCCommon;
using BCData.Common.News.NewsContent;
using BCData.Common.News.NewsContentComment;
using BCDto.Common.News.NewsContentComment;
using BCDto.Website.User;
using BCEntity.Common.EntityNameConst;
using BCEntity.Common.News.NewsContentComment;
using BCService.Utils;
using System;
using System.Collections.Generic;

namespace BCService.Common.News.NewsContentCommentService
{
    public class NewsContentCommentService : INewsContentCommentService
    {
        private readonly INewsContentCommentData newsContentCommentData;
        private readonly INewsContentData newsContentData;
        private readonly IRedisService redisService;
        private readonly string key = string.Format("{0}", EntityNameConst.NewsContentCommentEntity);

        public NewsContentCommentService(IRedisService redisService, 
            INewsContentCommentData newsContentCommentData,
            INewsContentData newsContentData)
        {
            this.redisService = redisService;
            this.newsContentCommentData = newsContentCommentData;
            this.newsContentData = newsContentData;
        }

        public PageableList<NewsContentCommentDto> GetList(long newsContentId, int index, int size, int? userId)
        {
            int dataCount;

            var list = newsContentCommentData.GetPageableList(newsContentId, userId, index, size, out dataCount);

            return new PageableList<NewsContentCommentDto>
            {
                Count = dataCount,
                Items = list.As<IEnumerable<NewsContentCommentDto>>()
            };
        }

        public PageableList<NewsContentCommentDto> GetChildrenCommentByCommentId(long newsContentId, long newsContentCommentId, int index, int size)
        {
            var list = newsContentCommentData.GetChildrenCommentByCommentId(newsContentId, newsContentCommentId, index, size, out int dataCount);
            return new PageableList<NewsContentCommentDto>
            {
                Count = dataCount,
                Items = list.As<IEnumerable<NewsContentCommentDto>>()
            };
        }

        public NewsContentCommentDto Create(UserDto current, NewsContentCommentRequestDto dto)
        {
            if (dto.ParentCommentId == null)
            {
                dto.ParentCommentId = 0;
            }
            var entity = dto.As<NewsContentCommentEntity>();
            entity.UserName = current.UserName;
            entity.UserId = current.UserId;
            entity.IsAnonymous = false;

            var result = this.newsContentCommentData.Add(entity).As<NewsContentCommentDto>();
            this.newsContentData.UpdateReplyCount(result.NewsContentId);
            return result;
        }

        public NewsContentCommentDto Get(long newsContentCommentId)
        {
            return this.newsContentCommentData.Get(newsContentCommentId).As<NewsContentCommentDto>();
        }

        public PageableList<NewsContentCommentDto> GetAllNewsContentCommentList(string title, string comment, DateTime? start, DateTime? end, bool? ascending, int index, int size)
        {
            var result = newsContentCommentData.GetAllNewsContentComments(title,comment,start,end,out int dataCount,ascending,index, size);
            return new PageableList<NewsContentCommentDto>
            {
                Count = dataCount,
                Items = result.As<IEnumerable<NewsContentCommentDto>>()
            };
        }

        public NewsContentCommentDto UpdateCommentLikeCount(long newsContentCommentId, bool isAdd)
        {
           return this.newsContentCommentData.UpdateCommentLikeCount(newsContentCommentId, isAdd).As<NewsContentCommentDto>();
        }

        public void Delete(long newsContentCommentId)
        {
            this.newsContentCommentData.Delete(newsContentCommentId);
        }

        #region 检查新闻内容评论是否存在
        public bool Exists(long newsContentCommentId)
        {
            return this.newsContentCommentData.Exists(newsContentCommentId).GetAwaiter().GetResult();
        }
        #endregion

        #region 启用新闻内容评论
        public bool Enabled(long newsContentCommentId)
        {
            if (!this.Exists(newsContentCommentId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.newsContentCommentData.Enabled(newsContentCommentId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, newsContentCommentId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 禁用新闻内容评论
        public bool Disabled(long newsContentCommentId)
        {
            if (!this.Exists(newsContentCommentId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.newsContentCommentData.Disabled(newsContentCommentId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, newsContentCommentId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion
    }
}
